package com.jp.knowledge.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.View;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.util.QRCodeUtils;
import com.jp.knowledge.util.ShareBuilder;
import com.jp.knowledge.view.SquareImageView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.media.UMImage;

import org.xutils.view.annotation.ViewInject;

/**
 * Created by 528787067@qq.com on 2017/7/11.
 */

public class QrCodeShowActivity extends SlidingActivity implements View.OnClickListener {

	@ViewInject(R.id.title)
	private TextView titleTv;
	@ViewInject(R.id.invite_qr_code)
	private SquareImageView qrCodeImg;
	@ViewInject(R.id.remind_text)
	private TextView shareRemindBtn;
	@ViewInject(R.id.invite_share)
	private TextView shareBtn;

	private String url;
	private String title;
	private String content;
	private String img;
	private String remind;
	private ShareAction shareAction;

	public static void openQRCode(Context context,
								  String url,
								  String title,
								  String content,
								  String img, String remind){
		context.startActivity(new Intent(context, QrCodeShowActivity.class)
				.putExtra("url", url)
				.putExtra("title", title)
				.putExtra("content", content)
				.putExtra("img", img)
				.putExtra("remind", remind));
	}

	@Override
	protected int getResourceId() {
		return R.layout.fragment_invite_people;
	}

	@Override
	protected void init() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			getWindow().setStatusBarColor(mContext.getResources().getColor(R.color.translate));
			getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
		}

		url = getIntent().getStringExtra("url");
		title = getIntent().getStringExtra("title");
		content = getIntent().getStringExtra("content");
		img = getIntent().getStringExtra("img");
		remind = getIntent().getStringExtra("remind");
		if(title != null){
			titleTv.setVisibility(View.VISIBLE);
			titleTv.setText(title);
		} else{
			titleTv.setVisibility(View.GONE);
		}
		if(remind != null) {
			shareRemindBtn.setVisibility(View.VISIBLE);
			shareRemindBtn.setText(remind);
			shareRemindBtn.setTextColor(getResources().getColor(R.color.font_gray_light));
		} else{
			shareRemindBtn.setVisibility(View.GONE);
		}

		ShareBuilder shareBuilder = new ShareBuilder(QrCodeShowActivity.this)
				.withTitle(title == null ? mContext.getResources().getString(R.string.app_name) : title)
				.withText(content)
				.withTargetUrl(url)
				.withCopyContent(title == null ? content : ("【" + title + "】 " + content))
				.withQRCodeUrl(url);
		try{
			shareBuilder.withMedia(new UMImage(mContext, img));
		} catch (Exception e){
			shareBuilder.withMedia(new UMImage(mContext, R.mipmap.icon_launcher));
		} finally {
			shareAction = shareBuilder.build();
		}

		QRCodeUtils.encodeAsBitmap(url, new QRCodeUtils.EncodeListener() {
			@Override
			public void encodeSuccedd(Bitmap qrCode) {
				qrCodeImg.setImageBitmap(qrCode);
			}
			@Override
			public void encodeError(Throwable e) {
				qrCodeImg.setImageResource(R.mipmap.morentutouxiang);
			}
		});

		contentView.setOnClickListener(this);
		shareBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if(v == contentView)
			onBackPressed();
		else if(v == shareBtn)
			shareAction.open();
	}
}
